package com.warm.system.controller.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.warm.core.domain.AjaxResult;
import com.warm.core.page.BaseController;
import com.warm.core.page.TableDataInfo;
import com.warm.excel.poi.utils.ExcelUtil;
import com.warm.log.annotation.Log;
import com.warm.log.enums.BusinessType;
import com.warm.orm.domain.sys.SysLogininfor;
import com.warm.system.service.ISysLogininforService;
import com.warm.system.service.SysPasswordService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.Valid;

import java.util.List;

/**
 * 系统访问记录
 *
 * @author warm
 */
@Valid
@Controller
@Mapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Inject
    private ISysLogininforService logininforService;

    @Inject
    private SysPasswordService passwordService;

    public SysLogininforController() {
    }

    @SaCheckPermission("monitor:logininfor:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysLogininfor> list(SysLogininfor logininfor) {
        return logininforService.selectLogininforPage(logininfor);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @SaCheckPermission("monitor:logininfor:export")
    @Post
    @Mapping("/export")
    public void export(Context ctx, SysLogininfor logininfor) {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
        util.exportExcel(ctx, list, "登录日志");
    }

    @SaCheckPermission("monitor:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @Delete
    @Mapping("/{infoIds}")
    public AjaxResult remove(Long[] infoIds) {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

    @SaCheckPermission("monitor:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @Delete
    @Mapping("/clean")
    public AjaxResult clean() {
        logininforService.cleanLogininfor();
        return success();
    }

    @SaCheckPermission("monitor:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @Get
    @Mapping("/unlock/{userName}")
    public AjaxResult unlock(String userName) {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
